﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using GYCTOAManagementSystemNewApi.Model;
using GYCTOAManagementSystemNewApi.Model.Tool;
using GYCTOAManagementSystemNewApi.Public;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace GYCTOAManagementSystemNewApi.Controllers
{
    /// <summary>
    /// 主页控制器
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class HomeController : Controller
    {
        DBContext db;
        InterfaceTest _ab;
        public HomeController(DBContext _db, InterfaceTest ab)
        {
            _ab = ab;
            db = _db;
        }
        /// <summary>
        /// 测试接口
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("test")]
        public JsonResult test()
        {
            return new JsonResult(_ab.Post());
        }
        /// <summary>
        /// 测试接口
        /// </summary>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpGet("test1")]
        public JsonResult test1()
        {
            return new JsonResult(1324);
        }
        [HttpPost("test2")]
        public JsonResult test2()
        {
            var a= 1;
            return new JsonResult(22);
        }

        /// <summary>
        /// 获取数据页面目录
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetInitMenuList")]
        public JsonResult GetInitMenuList()
        {
            var author = HttpContext.Request.Headers["Authorization"].ToString().Split(' ')[1];
            var PowerNumber = Convert.ToString(TokenManage.GetTokenValues(author)[2].Value);
            string[] PowerNumberarray = PowerNumber.Split(new char[] { ',' });
            var roleLiost = db.rolemenu.Where(x => !x.Delete).ToList();
            List<rolemenu> viewrolemenu = new List<rolemenu>();
            //超级管理员
            if ((PowerNumberarray).Contains("Admin"))
            {
                foreach (var item in roleLiost)
                {
                    if (item.ParentId == "0")
                    {
                        item.children = roleLiost.Where(x => x.ParentId.Equals(item.Roid)).ToList();
                        foreach (var itemch in item.children)
                        {
                            itemch.children = roleLiost.Where(x => x.ParentId.Equals(itemch.Roid)).ToList();
                        }
                        item.children = item.children.OrderBy(x => x.index).ToList();
                        viewrolemenu.Add(item);
                    }
                }
            }
            else
            {
                var listRole = roleLiost.Where(x => PowerNumberarray.Contains(x.Roid)).ToList();
                if (listRole != null)
                {
                    foreach (var item in listRole)
                    {
                        if (item.ParentId == "0")
                        {
                            item.children = roleLiost.Where(x => x.ParentId.Equals(item.Roid)).ToList();
                            foreach (var itemch in item.children)
                            {
                                itemch.children = roleLiost.Where(x => x.ParentId.Equals(itemch.Roid)).ToList();
                            }
                            item.children = item.children.OrderBy(x => x.index).ToList();
                            viewrolemenu.Add(item);
                        }
                        else
                        {
                            var par = roleLiost.Where(x => x.Roid.Equals(item.ParentId)).FirstOrDefault();
                            if (par!=null)
                            {
                                var listpar = viewrolemenu.Where(x => x.Roid.Equals(par.Roid)).FirstOrDefault();
                            if (listpar != null)
                            {
                                if (listpar.children.Where(x => x.Roid.Equals(item.Roid)).FirstOrDefault() == null)
                                {
                                    rolemenu s = item;
                                    listpar.children.Add(s);
                                }
                            }
                            else if (par.ParentId == "0")
                            {
                                rolemenu s = item;
                                par.children.Add(s);
                                viewrolemenu.Add(par);
                            }
                            }
                        }
                    }
                }
            }
            viewrolemenu = viewrolemenu.Distinct().Where(x => !string.IsNullOrWhiteSpace(x.Roid)).OrderBy(x => x.index).ToList();
            return new JsonResult(new { data = viewrolemenu });

        }
    }
}